package com.csw.android.dev_utils.ex

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.TypedValue
import androidx.annotation.AttrRes
import com.csw.android.dev_utils.R
import java.lang.RuntimeException


/**
 * Context扩展
 */
object ContextEx {

    /**
     * 获取主题颜色
     * @param resId 例如[R.attr.colorPrimary]、[R.attr.colorOnPrimary]
     */
    fun Context.getThemeColor(@AttrRes resId: Int): Int {
        val typedValue = TypedValue()
        theme.resolveAttribute(resId, typedValue, true)
        return typedValue.data
    }

    /**
     * 获取系统drawable
     * @param resId 例如[R.attr.selectableItemBackground]
     */
    fun Context.getSystemDrawable(@AttrRes resId: Int): Drawable {
        val typedValue = TypedValue()
        theme.resolveAttribute(resId, typedValue, true)
        return theme.obtainStyledAttributes(typedValue.resourceId, intArrayOf(resId)).getDrawable(0)
            ?: throw RuntimeException("can not found ${resId}")
    }


}